package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.MemberDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberService {

    @Autowired
    MemberDAO memberDAO;

    public ResponseBean queryUserList(RequestBean requestBean) {
        Integer currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        Integer pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        String nickName = requestBean.getRequestParamString("nickName");
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(memberDAO.queryUserPage(page, nickName));
        List<Map<String, Object>> rankNames = memberDAO.queryRankNames();
        responseBean.addResultPageItem("userInfo", page);
        responseBean.addResultItem("rankNames", rankNames);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseBean deleteUser(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        ResponseBean responseBean = requestBean.getResponseBean();
        try{
            int count = memberDAO.deleteUserById(userId);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        return responseBean;
    }

    public ResponseBean updateUser(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        String userTypeStr = requestBean.getRequestParamString("userType");
        String nickName = requestBean.getRequestParamString("nickName");
        String password = requestBean.getRequestParamString("password");
        String question = requestBean.getRequestParamString("question");
        String answer = requestBean.getRequestParamString("answer");
        String isValidatedStr = requestBean.getRequestParamString("isValidated");
        String userMoneyStr = requestBean.getRequestParamString("userMoney");
        String frozenMoneyStr = requestBean.getRequestParamString("frozenMoney");
        Integer payPoints = requestBean.getRequestParamInteger("payPoints");
        Integer rankPoints = requestBean.getRequestParamInteger("rankPoints");
        Integer visitCount = requestBean.getRequestParamInteger("visit_count");
        Integer userRank = requestBean.getRequestParamInteger("userRank");
        if(userRank==null){
            userRank=0;
        }
        Double userMoney = Double.valueOf(userMoneyStr);
        Double frozenMoney = Double.valueOf(frozenMoneyStr);
        Integer userType = null;
        Integer isValidated = null;
        if ("分销会员".equals(userTypeStr)) {
            userType = 1;
        } else {
            userType = 0;
        }
        if ("生效".equals(isValidatedStr)) {
            isValidated = 1;
        } else {
            isValidated = 0;
        }
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("userId", userId);
        userMap.put("userType", userType);
        userMap.put("nickName", nickName);
        userMap.put("password", password);
        userMap.put("question", question);
        userMap.put("answer", answer);
        userMap.put("isValidated", isValidated);
        userMap.put("userMoney", userMoney);
        userMap.put("frozenMoney", frozenMoney);
        userMap.put("payPoints", payPoints);
        userMap.put("rankPoints", rankPoints);
        userMap.put("visitCount", visitCount);
        userMap.put("userRank", userRank);
        ResponseBean responseBean = requestBean.getResponseBean();
        try{
            int count = memberDAO.updateUserById(userMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        return responseBean;
    }

}
